﻿Imports LSW.Win32

Namespace Forms
    Public Class QQForm
        Inherits FormBase
#Region "变量"
        ''' <summary>
        ''' 边框图片
        ''' </summary>
        Private _borderImage As Drawing.Image = My.Resources.fringe_bkg
#End Region

#Region "构造函数"
        ''' <summary>
        ''' 
        ''' </summary>
        Public Sub New()

            MyBase.New()
        End Sub

#End Region

#Region "属性"
        ''' <summary>
        ''' 
        ''' </summary>
        Protected Overrides ReadOnly Property MaxRect() As Rectangle
            Get
                Return New Rectangle(Me.Width - Me.CloseRect.Width - 28, -1, 28, 20)
            End Get
        End Property
        ''' <summary>
        ''' 
        ''' </summary>
        Protected Overrides ReadOnly Property MiniRect() As Rectangle
            Get
                Dim x As Integer = Me.Width - Me.CloseRect.Width - Me.MaxRect.Width - 28
                Dim rect As New Rectangle(x, -1, 28, 20)
                'return new Rectangle(this.Width - this.CloseRect.Width - 28, -1, 28, 20);
                Return rect
            End Get
        End Property
        ''' <summary>
        ''' 
        ''' </summary>
        Protected Overrides ReadOnly Property SysBtnRect() As Rectangle
            Get
                If MyBase._sysButton = ESysButton.Normal Then
                    Return New Rectangle(Me.Width - 28 * 2 - 39, 0, 39 + 28 + 28, 20)
                ElseIf MyBase._sysButton = ESysButton.Close_Mini Then
                    Return New Rectangle(Me.Width - 28 - 39, 0, 39 + 28, 20)
                Else
                    Return Me.CloseRect
                End If
            End Get
        End Property
        ''' <summary>
        ''' 
        ''' </summary>
        Protected Overrides ReadOnly Property CloseRect() As Rectangle
            Get
                Return New Rectangle(Me.Width - 39, -1, 39, 20)
            End Get
        End Property

#End Region

#Region "方法"
        ''' <summary>
        ''' 绘画按钮
        ''' </summary>
        ''' <param name="g">画板</param>
        ''' <param name="mouseState">鼠标状态</param>
        ''' <param name="rect">按钮区域</param>
        ''' <param name="str">图片字符串</param>
        Private Sub DrawButton(g As Graphics, mouseState As EMouseState, rect As Rectangle, str As String)
            Select Case mouseState
                Case EMouseState.Normal
                    g.DrawImage(AssemblyHelper.GetImage("btn_" & str & "_normal.png"), rect)
                    Exit Select
                Case EMouseState.Move, EMouseState.Up
                    g.DrawImage(AssemblyHelper.GetImage("btn_" & str & "_highlight.png"), rect)
                    Exit Select
                Case EMouseState.Down
                    g.DrawImage(AssemblyHelper.GetImage("btn_" & str & "_down.png"), rect)
                    Exit Select
            End Select
        End Sub
#End Region

#Region "Override Methods"
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="e"></param>
        Protected Overrides Sub OnPaint(e As PaintEventArgs)
            Dim g As Graphics = e.Graphics

            '绘画系统控制按钮
            Select Case MyBase.SysButton
                Case ESysButton.Normal
                    Me.DrawButton(g, MyBase.MinState, Me.MiniRect, "mini")
                    Me.DrawButton(g, MyBase.CloseState, Me.CloseRect, "close")
                    If Me.WindowState = FormWindowState.Maximized Then
                        Me.DrawButton(g, MyBase.MaxState, Me.MaxRect, "restore")
                    Else
                        Me.DrawButton(g, MyBase.MaxState, Me.MaxRect, "max")
                    End If
                    Exit Select
                Case ESysButton.Close
                    Me.DrawButton(g, MyBase.CloseState, Me.CloseRect, "close")
                    Exit Select
                Case ESysButton.Close_Mini
                    Me.DrawButton(g, MyBase.MinState, Me.MiniRect, "mini")
                    Me.DrawButton(g, MyBase.CloseState, Me.CloseRect, "close")
                    Exit Select
            End Select

            '绘画边框
            g.DrawImage(Me._borderImage, New Rectangle(0, 0, 10, 10), New Rectangle(5, 5, 10, 10), GraphicsUnit.Pixel)
            '左上角
            g.DrawImage(Me._borderImage, New Rectangle(0, -5, 10, Me.Height + 10), New Rectangle(5, 5, 10, Me._borderImage.Height - 10), GraphicsUnit.Pixel)
            '左边框
            g.DrawImage(Me._borderImage, New Rectangle(-5, Me.Height - 10, 10, 10), New Rectangle(0, Me._borderImage.Height - 15, 10, 10), GraphicsUnit.Pixel)
            '左下角
            g.DrawImage(Me._borderImage, New Rectangle(Me.Width - 9, -5, 10, 10), New Rectangle(20, 0, 10, 10), GraphicsUnit.Pixel)
            '右上角
            g.DrawImage(Me._borderImage, New Rectangle(Me.Width - 9, -5, 10, Me.Height + 10), New Rectangle(20, 5, 10, Me._borderImage.Height - 10), GraphicsUnit.Pixel)
            '右边框
            g.DrawImage(Me._borderImage, New Rectangle(Me.Width - 9, Me.Height - 10, 10, 10), New Rectangle(20, Me._borderImage.Height - 15, 10, 10), GraphicsUnit.Pixel)
            '右下角
            g.DrawImage(Me._borderImage, New Rectangle(5, -5, Me.Width - 10, 18), New Rectangle(12, 0, 6, 18), GraphicsUnit.Pixel)
            g.DrawImage(Me._borderImage, New Rectangle(5, Me.Height - 6, Me.Width - 10, 18), New Rectangle(12, 0, 6, 18), GraphicsUnit.Pixel)

            MyBase.OnPaint(e)
        End Sub

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="e"></param>
        Protected Overrides Sub OnResize(e As EventArgs)
            MyBase.OnResize(e)
            '调用API，将窗体剪成圆角
            Dim rgn As Integer = Gdi32.CreateRoundRectRgn(0, 0, Me.Width + 1, Me.Height + 1, 4, 4)
            User32.SetWindowRgn(Me.Handle, rgn, True)
        End Sub

#End Region
    End Class
End Namespace